<?php
/**
  * Mar 5, 2008
  *
  * Coded by: Ropu
  *
  */

class AppDAO {
  
public static function get(App &$app) {
    $db = DB::getDB();
    $sql = "SELECT id, name, hacker_id, title, description, thumb FROM app 
            WHERE id = ?";
    if($stmt = $db->prepare($sql)) {
      $stmt->bind_param("i", $app->getId());
      $stmt->bind_result($id, $name, $hacker_id, $title, $description, $thumb);
      $stmt->execute();
      if(!$stmt->fetch()) {
        $stmt->close();
        throw new Exception('Invalid app with id: ' . $app->getId());
      }
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new Exception($error);
      }
      $stmt->close();
      $app->setName($name);
      $app->setHackerId($hacker_id);
      $app->setTitle($title);
      $app->setDescription($description);
      $app->setThumb($thumb);
    }
    return $app->getId();
  }
  
  public static function getByHacker($hacker_id) {
    $db = DB::getDB();
    $apps = array();
    $apps_ids = array();
    $sql = "SELECT id FROM app 
            WHERE hacker_id = ? order by id desc";
    if($stmt = $db->prepare($sql)) {
      $stmt->bind_param("i", $hacker_id);
      $stmt->bind_result($id);
      $stmt->execute();
      
      while($stmt->fetch()) {
        $apps_ids[] = $id;
      }
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new Exception($error);
      }
      $stmt->close();
      foreach($apps_ids as $id) {
        $apps[$id] = new App($id);
      }
    }
    return $apps;
  }
  
  public static function getFavoriteByHacker($hacker_id) {
    $db = DB::getDB();
    $apps = array();
    $apps_ids = array();
    $sql = "SELECT app_id FROM favorite_app 
            WHERE hacker_id = ?";
    if($stmt = $db->prepare($sql)) {
      $stmt->bind_param("i", $hacker_id);
      $stmt->bind_result($id);
      $stmt->execute();
      
      while($stmt->fetch()) {
        $apps_ids[] = $id;
      }
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new Exception($error);
      }
      $stmt->close();
      foreach($apps_ids as $id) {
        $apps[$id] = new App($id);
      }
    }
    return $apps;
  }
  
  public static function getRate(App $app) {
    $db = DB::getDB();
    $sql = "SELECT avg(rate) rate FROM rate 
            WHERE app_id = ?";
    if($stmt = $db->prepare($sql)) {
      $stmt->bind_param("i", $app->getId());
      $stmt->bind_result($rate);
      $stmt->execute();
      if(!$stmt->fetch()) {
        $stmt->close();
        /*
         * Not rated yet
         */
        return -1;
      }
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new Exception($error);
      }
      $stmt->close();
    }
    return $rate;
  }
  public static function update(App &$app) {
    $db = DB::getDB();
    if($app->getHackerId() == $_SESSION['hacker']->getId()) {
      if ($stmt = $db->prepare("UPDATE  `app` SET 
                                        `name` = ?,
                                        `title` = ?,
                                        `description` = ?,
                                        `thumb` = ? 
                                        WHERE `id` = ? LIMIT 1")) {
        $stmt->bind_param("sssss",  $app->getName(), 
                                    $app->getTitle(), $app->getDescription(), 
                                    $app->getThumb(), $app->getId());
                
        $stmt->execute();
        if(Log::medium($stmt->error)) {
          $error = $stmt->error;
          $stmt->close();
          throw new DBException($error);
        }
        $stmt->close();
      }
      return $app->getId();
    }
    return -1;     
  }

  public static function insert(App &$app) {
    $db = DB::getDB();
    $user_id = -1;
    if ($stmt = $db->prepare("INSERT INTO  `app` (
                                            `name` ,
                                            `hacker_id` ,
                                            `title` ,
                                            `description` ,
                                            `thumb`
                                            )
                                            VALUES (
                                            ?,?,?,?,?
                                            )")) {
      $stmt->bind_param("sisss",  $app->getName(), $app->getHackerId(), 
                                  $app->getTitle(), $app->getDescription(), 
                                  $app->getThumb());
      $stmt->execute();
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new DBException($error);
      }
      $stmt->close();
    }
    return $db->insert_id;
  }
  
  public static function addFavorite($app_id, $hacker_id) {
    $db = DB::getDB();
    if ($stmt = $db->prepare("INSERT INTO `favorite_app` (
                                    `app_id`,
                                    `hacker_id`
                                    )
                                    VALUES (
                                    ?,?
                                    ) ")) {
      $stmt->bind_param("ii",  $app_id, $hacker_id);
      $stmt->execute();
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new DBException($error);
      }
      $stmt->close();
    }
    return true;
  }
  
  public static function removeFavorite($app_id, $hacker_id) {
    $db = DB::getDB();
    if ($stmt = $db->prepare("DELETE FROM `favorite_app` 
                                  WHERE
                                      `app_id` = ? 
                                    AND
                                      `hacker_id` = ? ")) {
      $stmt->bind_param("ii", $app_id, $hacker_id);
      $stmt->execute();
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new DBException($error);
      }
      $rta = $stmt->affected_rows;
      $stmt->close();
    }
    return $rta;
  }

  public static function rate($app_id, $rate, $hacker_id) {
    $db = DB::getDB();
    if ($stmt = $db->prepare("INSERT INTO `rate` (
                                    `app_id`,
                                    `rate`,
                                    `hacker_id`
                                    )
                                    VALUES (
                                    ?,?,?
                                    ) ")) {
      $stmt->bind_param("iii",  $app_id, $rate, $hacker_id);
      $stmt->execute();
      if(Log::medium($stmt->error)) {
        $error = $stmt->error;
        $stmt->close();
        throw new DBException($error);
      }
      $stmt->close();
    }
    return true;
  }  
  
}
